package com.ego.item.controller;

import com.ego.common.PageResult;
import com.ego.item.service.GoogsService;
import com.ego.item.vo.SpuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/goods")
@CrossOrigin
public class GoodsController {
    @Autowired
    private GoogsService goodsService;
    @GetMapping("/spu/page")
    public ResponseEntity<PageResult<SpuVo>> page(@RequestParam("key") String key,
                                                  @RequestParam("saleable") Boolean saleable,
                                                  @RequestParam(value = "page",defaultValue = "1") Integer page,
                                                  @RequestParam (value = "rows",defaultValue = "10") Integer rows){
        PageResult<SpuVo> result = goodsService.page(key,saleable,page,rows);
        if (result==null){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(result);
    }

    @DeleteMapping
    public ResponseEntity<Void> delete(@RequestParam("id") Long id){
        goodsService.deleteById(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 保存新增商品信息
     * @param spuVo
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> save(@RequestBody SpuVo spuVo){
        goodsService.save(spuVo);
        return ResponseEntity.ok().build();
    }

}
